﻿using BLL;
using EncryptHelper;
using gfLibrary;
using IYAKASAPortal.Login;
using IYAKASAPortal.Services;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal.Charge
{
    public partial class ChargeSF : System.Web.UI.Page
    {
        private UserInfo userInfo { get; set; }
        private static List<ListItem> lstItems { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
            {
                string userName = Session["UserName"].ToString();
                userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }

            if (!IsPostBack)
            {
                BindData();;
            }
        }

        private void BindData()
        {
            lstItems = new List<ListItem>();
            CONST_DEFINE_WEB_BLL bll = new CONST_DEFINE_WEB_BLL();
            CONST_DEFINE_BLL info = new CONST_DEFINE_BLL();

            ListItem item;

            var hpzl = info.GetModelList("CON_CHNAME='HPZL' order by CON_ORDER asc");
            foreach (var v in hpzl)
            {
                item = new ListItem();
                item.Text = v.CON_CON;
                item.Value = v.CON_CODE;
                drpHPZL.Items.Add(item);
            }

            var sfxm=bll.GetModelList("CategoryID='1'");
            foreach (var v in sfxm)
            {
                item = new ListItem();
                item.Text = v.Name+"("+v.ValueOfName+")";
                item.Value = v.ValueOfName+","+v.Code;
                chbFY.Items.Add(item);
            }
            //List<LoginHelper.ControlNames> lstCtl = new List<LoginHelper.ControlNames>();
            //List<Control> lstControls = new List<Control>();
            //LoginHelper.ControlNames ctlHPHM = new LoginHelper.ControlNames();
            //ctlHPHM.Name = "txtHPHM";
            //ctlHPHM.Value = "";
            //lstCtl.Add(ctlHPHM);
            //lstControls.Add(txtHPHM);
        }

        protected void txtHPHM_TextChanged(object sender, EventArgs e)
        {
            btnCharge.CssClass = "btnChargeEnabled";
            btnCharge.Enabled = true;
        }

        protected void drpHPZL_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnCharge.CssClass = "btnChargeEnabled";
            btnCharge.Enabled = true;
        }

        protected void chbFY_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnCharge.CssClass = "btnChargeEnabled";
            btnCharge.Enabled = true;
            gfAjaxJs.SetButtonMsg(btnCharge, "是否确认收费?");
            decimal total = 0;
            for (int i = 0; i < chbFY.Items.Count; i++)
            {
                if (chbFY.Items[i].Selected)
                {
                    
                    string[] values = chbFY.Items[i].Value.Split(',');
                    total += decimal.Parse(values[0]);
                }
            }
            txtFYZJE.Text = total.ToString();
        }

        protected void btnCharge_Click(object sender, EventArgs e)
        {
            InsertCharge();
            btnCharge.CssClass = "btnChargeDisabled";
            btnCharge.Enabled = false;
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 添加一条收费记录
        /// </summary>
        private void InsertCharge()
        {
            CHARGE_INFO_BLL bll = new CHARGE_INFO_BLL();
            CHARGE_INFO bill = new CHARGE_INFO();
            bill.HPHM = txtHPHM.Text;
            bill.HPZL = drpHPZL.SelectedItem.Text;
            bill.HPZLDH = drpHPZL.SelectedItem.Value;

            Random rand = new Random();
            int randNum = rand.Next(1, 60);
            string key = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + randNum.ToString();
            string unique = Encrypt.MD5Encrypt(key).Substring(2, 5);
            bill.SFLSH = "F" + DateTime.Now.ToString("yyMMdd") + "P" + bill.HPZLDH + unique;

            bill.SFZL = "收费";
            bill.SFZLDH = "1";
            bill.SFZJE = decimal.Parse(txtFYZJE.Text);

            string SFXM = "";
            for (int i = 0; i < chbFY.Items.Count; i++)
            {
                if (chbFY.Items[i].Selected)
                {
                    string[] values = chbFY.Items[i].Value.Split(',');
                    SFXM += values[1] + ",";
                }
            }
            bill.SFXM = SFXM;

            bill.SFY = userInfo.UserName;
            bill.SFRQ = DateTime.Now;

            string strHPHM = txtHPHM.Text.Trim();
            if (strHPHM.Trim().Length == 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "号牌号码不能为空!");
                txtHPHM.Focus();
                return;
            }

            if (chbFY.SelectedIndex < 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "请选择收费项!");
                chbFY.Focus();
                return;
            }

            bool ok = bll.Insert(bill);
            if (ok)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "收费成功");
            }
            else
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "收费失败");
            }
        }

    }
}